package harbor_hook

import (
	"atomgit.com/openlinksaas-org/proto-gen-go.git/events/events_harbor"
)

type Resource struct {
	Digest      string `json:"digest"`
	Tag         string `json:"tag"`
	ResourceUrl string `json:"resource_url"`
}

type Repository struct {
	Name         string `json:"name"`
	Namespace    string `json:"namespace"`
	RepoFullName string `json:"repo_full_name"`
	RepoType     string `json:"repo_type"`
}

type HarborEventData struct {
	Resources  []Resource `json:"resources"`
	Repository Repository `json:"repository"`
}

func (item *HarborEventData) ToProtoBuf() *events_harbor.EventData {
	resources := []*events_harbor.Resource{}
	for _, res := range item.Resources {
		resources = append(resources, &events_harbor.Resource{
			Digest:      res.Digest,
			Tag:         res.Tag,
			ResourceUrl: res.ResourceUrl,
		})
	}

	return &events_harbor.EventData{
		Resources: resources,
		Repository: &events_harbor.Repository{
			Name:         item.Repository.Name,
			Namespace:    item.Repository.Namespace,
			RepoFullName: item.Repository.RepoFullName,
			RepoType:     item.Repository.RepoType,
		},
	}
}

type HarborHook struct {
	Type      string          `json:"type"`
	OccurAt   int64           `json:"occur_at"`
	Operator  string          `json:"operator"`
	EventData HarborEventData `json:"event_data"`
}

func (item *HarborHook) ToPushArtifactEvent() *events_harbor.PushArtifactEvent {
	return &events_harbor.PushArtifactEvent{
		OccurAt:   item.OccurAt * 1000,
		Operator:  item.Operator,
		EventData: item.EventData.ToProtoBuf(),
	}
}

func (item *HarborHook) ToDeleteArtifactEvent() *events_harbor.DeleteArtifactEvent {
	return &events_harbor.DeleteArtifactEvent{
		OccurAt:   item.OccurAt * 1000,
		Operator:  item.Operator,
		EventData: item.EventData.ToProtoBuf(),
	}
}

func (item *HarborHook) ToUploadChartEvent() *events_harbor.UploadChartEvent {
	return &events_harbor.UploadChartEvent{
		OccurAt:   item.OccurAt * 1000,
		Operator:  item.Operator,
		EventData: item.EventData.ToProtoBuf(),
	}
}

func (item *HarborHook) ToDeleteChartEvent() *events_harbor.DeleteChartEvent {
	return &events_harbor.DeleteChartEvent{
		OccurAt:   item.OccurAt * 1000,
		Operator:  item.Operator,
		EventData: item.EventData.ToProtoBuf(),
	}
}
